package com.bwie.orders.listen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.common.utils.R;
import com.bwie.orders.dao.OrderDao;
import com.bwie.orders.entity.OrderEntity;
import com.bwie.orders.feign.BargainFeign;
import com.bwie.orders.feign.GoodsFeign;
import com.bwie.orders.vo.ActivityVo;
import com.bwie.orders.vo.BargainVo;
import com.bwie.orders.vo.GoodsVo;
import com.bwie.orders.vo.IdVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class MyOrdersListener {
    @Autowired
    private GoodsFeign goodsFeign;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private BargainFeign bargainFeign;



    @RabbitListener(queues = "orders")
    @RabbitHandler
    public void addOrders(IdVo idVo, Message message, Channel channel) throws IOException {
        //feign调用 根据活动id获取活动详情
        OrderEntity orderEntity = new OrderEntity();
        R info = goodsFeign.info(idVo.getActivityId());
        ActivityVo activityVo = info.getData(new TypeReference<ActivityVo>() {
        });
        //feign调用 根据商品id获取商品信息
        R info2 = goodsFeign.info2(activityVo.getGoodsId());
        GoodsVo goodsVo = info2.getData(new TypeReference<GoodsVo>() {
        });
        orderEntity.setUserId(idVo.getUserId());//用户id
        orderEntity.setGoodsId(goodsVo.getGid());//商品id
        orderEntity.setActivityId(idVo.getActivityId());//活动id
        orderEntity.setGoodsName(goodsVo.getGoodsName());//商品名称
        orderEntity.setGoodsCount(1);//商品数量 默认为1件
        orderEntity.setGoodsPrice(activityVo.getMiaoshaPrice());//商品秒杀价格
        orderEntity.setGoodsChannel(2);//1.pc 2.android 3.ios 默认为安卓
        orderEntity.setStatus(0);//订单状态：0新建未支付，1已支付，2已发货，3已收货，4已退款，5已完成
        orderEntity.setCreateDate(new Date());//订单创建时间
        //随机数
        String code = "";
        Random random = new Random();
        for(int i = 0; i<10;i++){
            code+= random.nextInt(10);
        }
        orderEntity.setOrderno(code+"ZHIZHI");//生成订单编号 防止付款时订单重复
        System.out.println("秒杀成功");
        //添加订单
        orderDao.insert(orderEntity);
        //手动确认 防止消息丢失
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


//    @RabbitListener(queues = "orders2")
//    public void addOrders2(IdVo idVo, Message message, Channel channel) throws IOException {
//        //feign调用 根据活动id获取活动详情
//        OrderEntity orderEntity = new OrderEntity();
//        R info = bargainFeign.info3(idVo.getActivityId());
//        BargainVo bargainVo = info.getData(new TypeReference<BargainVo>() {
//        });
//        //feign调用 根据商品id获取商品信息
//        R info2 = goodsFeign.info2(bargainVo.getGoodsId());
//        GoodsVo goodsVo = info2.getData(new TypeReference<GoodsVo>() {
//        });
//        orderEntity.setUserId(idVo.getUserId());//用户id
//        orderEntity.setGoodsId(goodsVo.getGid());//商品id
//        orderEntity.setActivityId(idVo.getActivityId());//活动id
//        orderEntity.setGoodsName(goodsVo.getGoodsName());//商品名称
//        orderEntity.setGoodsCount(1);//商品数量 默认为1件
//        orderEntity.setGoodsPrice(bargainVo.getGoodsPrice());//商品原价
//        orderEntity.setGoodsChannel(2);//1.pc 2.android 3.ios 默认为安卓
//        orderEntity.setStatus(0);//订单状态：0新建未支付，1已支付，2已发货，3已收货，4已退款，5已完成
//        orderEntity.setCreateDate(new Date());//订单创建时间
//        //随机数
//        String code = "";
//        Random random = new Random();
//        for(int i = 0; i<10;i++){
//            code+= random.nextInt(10);
//        }
//        orderEntity.setOrderno(code+"ZHIZHI");//生成订单编号 防止付款时订单重复
//        System.out.println("生成订单成功");
//        //添加订单
//        orderDao.insert(orderEntity);
//        //手动确认 防止消息丢失
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }

}
